package com.hqq.rabbitmq.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hqq.rabbitmq.entity.Person;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * 广播消息模式的消费者
 * @author huangqiqin
 * @date 2021/04/19 00:37
 **/
@Component
public class FanoutConsumer {

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 监听队列一的消费者
     * @param msg
     */
    @RabbitListener(queues = "${mq.fanout.queue.one.name}", containerFactory = "singleListenerContainer")
    public void consumerFanoutMessageOne(@Payload byte[] msg){
        try {
            Person person = objectMapper.readValue(msg, Person.class);
            System.out.println("==> 接收到队列一的消息：" + person);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 监听队列二的消费者
     * @param msg
     */
    @RabbitListener(queues = "${mq.fanout.queue.two.name}", containerFactory = "singleListenerContainer")
    public void consumerFanoutMessageTwo(@Payload byte[] msg){
        try {
            Person person = objectMapper.readValue(msg, Person.class);
            System.out.println("==> 接收到队列二的消息：" + person);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
